import { defineStore } from 'pinia'
import { isObject } from '@admin/utils/index'
import { useRouter } from 'vue-router'

export const usePageStore = defineStore('page', {
  state: () => ({
    pageinfo: {},
    treeId: '',
    treeName: '',
    trees: [],
    currentTree: {},
    currentTab: '', // 待办/已办
    requestPending: false
  }),
  getters: {
    treeInfo(state) {
      try {
        return JSON.parse(state.pageinfo.treeInfo)
      } catch (e) {
        return {}
      }
    },
    tmp(state) {
      return state?.pageinfo?.tmp || ''
    },
    allTreeKeys(state) {
      return state.trees.filter(i => {
        return i[state.treeId] !== '0'
      }).map(i => {
        return i[state.treeId]
      })
    },
    allTreeNames(state) {
      return state.trees.filter(i => {
        return i[state.treeId] !== '0'
      }).map(i => {
        return i[state.treeName]
      })
    },
    clientMacro(state) {
      let macro = {}
      // 选中树
      if (state.currentTree && Object.keys(state.currentTree).length && state.treeId) {
        // if (this.tmp === '1120' && state.currentTree[state.treeId] == '0') {
        //   // 标签表格，全部(0)查询时传其他tab的值
        //   macro['treeKey'] = this.allTreeKeys.join(',')
        //   macro['treeName'] = this.allTreeNames.join(',')
        // } else {
        macro['treeKey'] = state.currentTree[state.treeId]
        macro['treeName'] = state.currentTree[state.treeName]
        // }
      } else {
        macro['treeKey'] = ''
        macro['treeName'] = ''
      }
      // 从配置中拼接宏变量
      if (this.treeInfo && this.treeInfo.macroConfig && isObject(this.treeInfo.macroConfig)) {
        const macroConfig = this.treeInfo.macroConfig
        Object.keys(macroConfig).forEach(treeKey => {
          macro[treeKey] = state.currentTree[macroConfig[treeKey]]
        })
      }
      // pageinfo获取参数
      if (state.pageinfo.customParam) {
        try {
          macro = Object.assign(macro, JSON.parse(state.pageinfo.customParam))
        } catch (e) { }
      }
      // 路由参数parentDataId
      const router = useRouter()
      const query = router?.app?._route?.query || {}
      Object.keys(query).forEach(key => {
        if (key === 'parentDataId' && query[key]) {
          macro['parentDataId'] = query[key]
        } else if (key === 'id' && query[key]) {
          macro['parentDataId'] = query[key]
        } else if (key.substr(0, 3) === 'cm_') {
          // cm_开头的key放到宏变量中
          const macroName = key.substr(3)
          macro[macroName] = query[key]
        }
      })
      // 待办已办页面
      if (state.currentTab === 'status0') {
        macro['processStatus'] = '0'
      } else if (state.currentTab === 'status1') {
        macro['processStatus'] = '1'
      }
      return macro
    }
  },
  actions: {
    setPageInfo(pageinfo) {
      let treeId = ''
      let treeName = ''
      try {
        if (pageinfo && pageinfo.treeInfo) {
          const treeInfo = JSON.parse(pageinfo.treeInfo)
          treeId = treeInfo.keyField || ''
          treeName = (treeInfo.displayFields && Array.isArray(treeInfo.displayFields) && treeInfo.displayFields.length) ? treeInfo.displayFields[0] : (treeInfo.displayFields || '')
        }
      } catch (e) { }
      this.treeId = treeId
      this.treeName = treeName
      this.pageinfo = pageinfo
      this.currentTree = {}
    },
    setTreeId(treeId) {
      this.treeId = treeId
    },
    setTreeName(treeName) {
      this.treeName = treeName
    },
    setCurrentTree(tree) {
      window.$CD.treeInfo = tree
      window.$CD.treeId = tree[this.treeId] || ''
      this.currentTree = tree
    },
    setCurrentTab(tab) {
      this.currentTab = tab
    },
    setTrees(trees) {
      this.trees = trees
    },
    setPending(pending) {
      this.requestPending = pending
    }
  }
})
